﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using WebUI.DAL;
using WebUI.BL.Products;

namespace WebUI.BL.ProductCategories
{
    public class ProductCategories
    {
        public void Insert(tbl_ProductCategory productCategory)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_ProductCategory newproductCategory = new tbl_ProductCategory()
                    {
                        Status=productCategory.Status,
                        Root=productCategory.Root,
                        ParentCategoryNo=productCategory.ParentCategoryNo,
                        Name=productCategory.Name,
                        Link=productCategory.Link,
                        Description=productCategory.Description,
                        CreatedUser=productCategory.CreatedUser,
                        CreatedDate=productCategory.CreatedDate,
                        CategoryImg=productCategory.CategoryImg,
                        CategoryCode=productCategory.CategoryCode,
                        BreadCrum=productCategory.BreadCrum,
                        DisplayOrder = productCategory.DisplayOrder
                    };
                    contextdb.tbl_ProductCategories.InsertOnSubmit(newproductCategory);
                    contextdb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void Update(tbl_ProductCategory productCategory)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var updateproductCategory = contextdb.tbl_ProductCategories.SingleOrDefault(u => u.CategoryNo == productCategory.CategoryNo);
                try
                {
                    if (updateproductCategory != null)
                    {
                        updateproductCategory.Status=productCategory.Status;
                        updateproductCategory.Root=productCategory.Root;
                        updateproductCategory.ParentCategoryNo=productCategory.ParentCategoryNo;
                        updateproductCategory.Name=productCategory.Name;
                        updateproductCategory.Link=productCategory.Link;
                        updateproductCategory.Description=productCategory.Description;
                        updateproductCategory.CreatedUser=productCategory.CreatedUser;
                        updateproductCategory.CreatedDate=productCategory.CreatedDate;
                        updateproductCategory.CategoryImg=productCategory.CategoryImg;
                        updateproductCategory.CategoryCode=productCategory.CategoryCode;
                        updateproductCategory.BreadCrum = productCategory.BreadCrum;

                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }

            }
        }
        public void Delete(int productCategoryNo)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteproductCategory = contextdb.tbl_ProductCategories.SingleOrDefault(u => u.CategoryNo == productCategoryNo);
                try
                {
                    if (deleteproductCategory != null)
                    {
                        contextdb.tbl_ProductCategories.DeleteOnSubmit(deleteproductCategory);
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void DeleteByStatus(int productCategoryNo)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteproductCategory = contextdb.tbl_ProductCategories.SingleOrDefault(u => u.CategoryNo == productCategoryNo);
                try
                {
                    if (deleteproductCategory != null)
                    {
                        deleteproductCategory.Status = "Deleted";
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }

        public IEnumerable<T> GetDropDownList<T>(Func<string,string,T> creator) where T:class
        {
            var dbcontext = new AdlerContextDataContext();
            var categories = dbcontext.tbl_ProductCategories.AsEnumerable();

                var result = categories.Select(u => creator(u.CategoryNo.ToString(), u.Name));
                return result;
        }
        public IEnumerable<tbl_ProductCategory> GetProductCategoryByName(string name)
        {
            var dbcontext = new AdlerContextDataContext();
            var result = dbcontext.tbl_ProductCategories.AsQueryable();
            if(!String.IsNullOrEmpty(name))
            {
                result = result.Where(x => x.Name.Contains(name));
            }

            var categoriesRelated = from n in result.AsEnumerable()
                                    select new tbl_ProductCategory()
                                               {
                                                   Name = n.Name,
                                                   Status = n.Status,
                                                   BreadCrum = ConvertChildIdToName(n.CategoryNo, GetChild(n.CategoryNo, result),result),
                                                   DisplayOrder = n.DisplayOrder,
                                                   CategoryNo = n.CategoryNo
                                                   
                                               };

            return categoriesRelated;
        }
        public string ConvertChildIdToName(int rootId, List<int> chilstChildId,IQueryable<tbl_ProductCategory> source )
        {
            var nameRoot = source.FirstOrDefault(x => x.CategoryNo == rootId);
            string display = nameRoot.Name + "->";

            foreach (var i in chilstChildId)
            {
                var child = source.FirstOrDefault(x => x.CategoryNo == i);
                display = display + child.Name + "->";
            }

            if(display.EndsWith("->"))
            {
                display=display.Substring(0,display.Length - 2);
            }

            return display;
        }
        public List<int> GetChild(int? idParent, IQueryable<tbl_ProductCategory> source )
        {

            var result = from x in source
                         where x.ParentCategoryNo == idParent
                         select x;
            var categoriesId = new List<int>();
            foreach (var c in result)
            {
                categoriesId.Add(c.CategoryNo);
                categoriesId.AddRange(GetChild(c.CategoryNo,source));
            }
            return categoriesId;
        }

        // Show Product By Product Category.
        public List<MODEL_SP_FONT_PRODUCT_CATEGORY> Get_Font_Category(int productcategoryno, int page, int pagesize)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                int index = 1;
                var query = from n in contextdb.SP_FONT_PRODUCT_CATEGORY(productcategoryno, page, pagesize)
                            select new MODEL_SP_FONT_PRODUCT_CATEGORY
                            {
                                STT = index++,
                                DisplayPrice = n.DisplayPrice,
                                ProductNo = n.ProductNo,
                                ProductCode = n.ProductCode,
                                ProductName = n.ProductName,
                                ProductLink = n.ProductLink,
                                ImgReview = n.ImgReview,
                                ImgSmall = n.ImgSmall,
                                ColorView = n.ColorView
                            };
                return query.ToList();
            }
        }
    }
}
